package com.goodomen.garbage.common.utils.md5;

import java.security.MessageDigest;

public class Md5Util {

    /**
     * md5 32位小写
     *
     * @param @param  str
     * @param @return
     * @param @throws Exception    设定文件
     * @return String    返回类型
     * @throws
     * @Title: EncoderByMd5
     * @Description: md5 32未小写
     */
    public static String EncoderByMd5(String str) {
        MessageDigest md5;
        StringBuffer buf = new StringBuffer();
        try {
            md5 = MessageDigest.getInstance("MD5");

            md5.update((str).getBytes("UTF-8"));
            byte b[] = md5.digest();
            int i;
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));

            }
        } catch (Exception e) {
            System.out.println("");
        }
        return buf.toString();

    }


    //通过获取参数并且生成新的MD5校验码 然后和 获取到的MD5校验码 对比 以此验证传过来的数据是否被修改
    public static boolean validatePassword(String origin, String inputString, String charsetname) {
        if (origin.equals(MD5Encode(inputString, charsetname).toUpperCase())) {
            return true;
        } else {
            return false;
        }
    }

    //通过获取数据来生成MD5验证码   charsetname为"UTF-8"编码比较好
    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = origin + "9ee30e43ed0424cc";//sangame.com
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digestString = md.digest(resultString.getBytes(charsetname));
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(digestString);
            else
                resultString = byteArrayToHexString(digestString);
        } catch (Exception exception) {
        }
        return resultString;
    }

    //生成MD5序列的逻辑
    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        String byteToHex = null;
        for (int i = 0; i < b.length; i++) {
            byteToHex = byteToHexString(b[i]);
            resultSb.append(byteToHex);
        }
        return resultSb.toString();
    }

    //逻辑中每一位数据 在这里对应转换成两个字符
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    //转换字符的集合 这里可以根据自己喜好改一下顺序 达到改变生成的MD5的结果的作用 慢慢试 估计可能可以清楚顺序的
    private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

//    public static void main(String[] args) {
//
//        System.out.println(Md5Util.MD5Encode("15384031327", "UTF-8"));
//    }

}
